from generic.artist import Artist
from tidal.constants import IMG_URL
from tidal.tidal_api import TidalAPI
import tidal.album
from cache.cache import cache
from cache.constants import TIDAL_ALBUM

class TidalArtist(Artist):
  def __init__(self, id, name):
    self.id = id
    self.name = name

  def set_raw_data(self, artist_data):
    self.id = artist_data["id"]
    self.name = artist_data["name"]

  def set_albums_raw_data(self, raw_data):
    def get_album(self, album_data):
      cache_key = TIDAL_ALBUM % album_data["id"]
      if cache.has(cache_key):
        album = cache.get(cache_key)
      else:
        album = tidal.album.TidalAlbum(artist_info["id"], artist_info["name"])
        cache.set(cache_key, artist)
      return artist
    self.albums = list(map(
      lambda a: tidal.album.TidalAlbum.grab_or_create(a["id"], raw_data = a),
      raw_data["items"]
    ))

  def get_full_data(self, api: TidalAPI):
    if not self.is_full:
      raw_data = api.get_artist_data(self.artist_id).json()
      self.set_raw_data(raw_data)
      albums_data = api.get_artist_albums_data(self.artist_id).json()
      self.set_albums_raw_data(albums_data)
    if self.albums != None:
      for album in self.albums:
        if not album.is_full:
          album.get_full_data(api)

  @property
  def picture_url(self, width = 512, height = 512):
    return IMG_URL.format(width = width, height = height,
                          id = self.artist_id, id_type = 'artistid')

  def set_values(self,
                 artist_id = None,
                 name = None,
                 albums = None,
                 raw_data = None,
                 is_full = False):
    if raw_data != None:
      self.set_raw_data(raw_data)
      return
    self.artist_id = artist_id
    self.name = name
    self.albums = albums
    self.is_full = is_full
